package models

import (
	"context"
	"go-chain-api/global"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type ApplyToken struct {
	Id          string `bson:"_id,omitempty" json:"id"`
	ChainName   string `bson:"chainName" json:"chainName"`
	ChainID     int64  `bson:"chainId" json:"chainId"`
	UserId      string `bson:"userId" json:"userId"`
	Address     string `bson:"address" json:"address"`
	Name        string `bson:"name" json:"name"`
	Symbol      string `bson:"symbol" json:"symbol"`
	Decimals    uint   `bson:"decimals" json:"decimals"`
	TotalSupply string `bson:"totalSupply" json:"totalSupply"`
	ApplyStatus string `bson:"applyStatus" json:"applyStatus"` // 0 申请中 1.成功 2.失败
	Timestamp   int64  `bson:"timestamp" json:"timestamp"`     //上币申请时间
}

func (a ApplyToken) Create(token ApplyToken) error {

	//var userToken UserToken
	//userTokenClient := global.DBEngine.Database(global.DbConfig.DbName).Collection("user_token")
	//
	//err := userTokenClient.FindOne(context.TODO(), bson.M{"address": token.Address, "chainName": token.ChainName, "chainId": token.ChainID}).Decode(&userToken)
	//if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
	//	return err
	//}
	//if userToken.Address != "" {
	//	return errors.New("address is exist")
	//}
	//
	//var scanConfig ScanConfig
	//configClient := global.DBEngine.Database(global.DbConfig.DbName).Collection("scan_config")
	//err = configClient.FindOne(context.TODO(), bson.M{"chainId": token.ChainID, "chainName": token.ChainName}).Decode(&scanConfig)
	//if err != nil {
	//	return err
	//}
	//client, err := ethclient.Dial(scanConfig.RpcUrl)
	//if err != nil {
	//	return err
	//}
	//
	//tokenName, err := contract.CallContractMethod(client, common.HexToAddress(token.Address), evmAbi.UNISWAP_V2_PAIR_ABI, "name")
	//if err != nil {
	//	return err
	//}
	//tokenSym, err := contract.CallContractMethod(client, common.HexToAddress(token.Address), evmAbi.UNISWAP_V2_PAIR_ABI, "symbol")
	//if err != nil {
	//	return err
	//}
	//tokenDec, err := contract.CallContractMethod(client, common.HexToAddress(token.Address), evmAbi.UNISWAP_V2_PAIR_ABI, "decimals")
	//if err != nil {
	//	return err
	//}
	//tokenTot, err := contract.CallContractMethod(client, common.HexToAddress(token.Address), evmAbi.UNISWAP_V2_PAIR_ABI, "totalSupply")
	//if err != nil {
	//	return err
	//}
	//
	//token.Decimals = uint(tokenDec[0].(uint8))
	//token.Name = tokenName[0].(string)
	//token.Symbol = tokenSym[0].(string)
	//token.TotalSupply = tokenTot[0].(*big.Int).String()
	//token.ApplyStatus = "0"

	applyTokenClient := global.DBEngine.Database(global.DbConfig.DbName).Collection("apply_token")
	_, err := applyTokenClient.InsertOne(context.TODO(), token)
	if err != nil {
		return err
	}
	return nil
}

func (a ApplyToken) GetApplyTokenList(userId string, pageIndex uint, pageSize uint, chainName string, address string) (error, []*ApplyToken, int64) {

	var applyTokenList []*ApplyToken
	tokenClient := global.DBEngine.Database(global.DbConfig.DbName).Collection("apply_token")

	optionsFind := options.Find()
	optionsFind.SetSkip(int64(pageIndex))
	optionsFind.SetLimit(int64(pageSize))
	optionsFind.SetSort(bson.D{{"_id", -1}})

	filter := bson.M{"userId": userId}
	if chainName != "" {
		filter["chainName"] = chainName
	}
	if address != "" {
		filter["address"] = address
	}
	find, err := tokenClient.Find(context.TODO(), filter, optionsFind)
	if err != nil {
		return err, nil, 0
	}

	err = find.All(context.TODO(), &applyTokenList)
	if err != nil {
		return err, nil, 0
	}

	documents, err := tokenClient.CountDocuments(context.TODO(), filter)
	if err != nil {
		return err, nil, 0
	}

	return nil, applyTokenList, documents

}
